﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace P2P.Network.ExtensionMethods
{

    public delegate Boolean SocketAsyncMethod(SocketAsyncEventArgs args);

    public static class ExtensionMethods
    {
        public static void InvokeAsyncMethod(this Socket socket, SocketAsyncMethod method, EventHandler<SocketAsyncEventArgs> callback, SocketAsyncEventArgs args)
        {
            if (!method(args))
                callback(socket, args);
        }

        public static int SendTo(this Socket socket,string buffer,string ip,int port) 
        {
            return socket.SendTo(Encoding.UTF8.GetBytes(buffer), new IPEndPoint(IPAddress.Parse(ip), port));
        }

        public static int SendTo(this Socket socket, string buffer, IPEndPoint endPoint)
        {
            return socket.SendTo(Encoding.UTF8.GetBytes(buffer), endPoint);
        }

        public static int Send(this Socket socket, string buffer)
        {
            return socket.Send(Encoding.UTF8.GetBytes(buffer));
        }
    }
}